﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Mortgage.Form
{
    public partial class ExpiredMortageBulk : System.Web.UI.Page
    {
        Mortgage.Srv.IMortgageSrv svcMortgage = Mortgage.Srv.SrvFactory.CreateMortgageSrv();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                InitDefaultValue();
            }
        }

        protected void InitDefaultValue()
        {
            fldFltMortdate.Text = Utility.FormatDateTime(DateTime.Now);
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                HandleSearch();
            }
        }

        protected void btnBulk_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                HandleBulk();
            }
        }

        protected void HandleBulk()
        {
            List<long> idLst = new List<long>();
            long id;
            foreach (GridViewRow r in grid.Rows)
            {
                CheckBox chk = r.FindControl("chkBulk") as CheckBox;
                id = Convert.ToInt64(grid.DataKeys[r.DataItemIndex].Value);
                if (chk.Checked)
                    idLst.Add(id);

            }
            svcMortgage.BulkExpired(idLst);
            lblMess.Text = idLst.Count + " records success! (thanh cong)";
            HandleSearch();

        }

        protected void HandleSearch()
        {
            DateTime dteExpired = new DateTime();
            Utility.ParseDateTime(fldFltMortdate.Text, out dteExpired);
            int ageId = Utility.AgentId;
            grid.DataSource = svcMortgage.FilterMortgageExpire(dteExpired, ageId);
            grid.DataBind();

            btnBulk.Enabled = true;
        }
    }
}